import vtkmodules.all as vtk

from CloudEngine.core.func.parse_attributes import parse_attributes


class IVTPReader:
    reader = None
    mapper = None
    attributes = None

    def __init__(self, options=None):
        self.mapper = vtk.vtkPolyDataMapper()
        self.reader = vtk.vtkXMLPolyDataReader()
        self.attributes = {}

    def _initMapper(self):
        self.mapper.SetInputConnection(self.reader.GetOutputPort())

    def read(self, file_path):
        self.reader.SetFileName(file_path)
        self.reader.Update()
        self._initMapper()
        self.get_cell_attribute()

    def get_poly_data(self):
        return self.reader.GetOutput()

    def get_cell_attribute(self):
        # 如果需要访问单元属性数据
        cell_data = self.get_poly_data().GetCellData()
        if cell_data and cell_data.GetNumberOfArrays() > 0:
            print("\n单元属性数据:")
            for i in range(cell_data.GetNumberOfArrays()):
                array = cell_data.GetArray(i)
                print(f"  属性 {i}: {array.GetName()}")
